#!/bin/bash

echo "=== API测试脚本 ==="
echo "请确保Spring Boot应用已启动在 http://localhost:8080"
echo ""

BASE_URL="http://localhost:8080/api/users"

# 测试查询所有用户
echo "1. 测试查询所有用户..."
curl -s -X GET "$BASE_URL" | jq '.' 2>/dev/null || curl -s -X GET "$BASE_URL"
echo ""

# 测试根据ID查询用户
echo "2. 测试根据ID查询用户..."
curl -s -X GET "$BASE_URL/1" | jq '.' 2>/dev/null || curl -s -X GET "$BASE_URL/1"
echo ""

# 测试根据用户名模糊查询
echo "3. 测试根据用户名模糊查询..."
curl -s -X GET "$BASE_URL/name/张" | jq '.' 2>/dev/null || curl -s -X GET "$BASE_URL/name/张"
echo ""

# 测试新增用户
echo "4. 测试新增用户..."
curl -s -X POST "$BASE_URL" \
  -H "Content-Type: application/json" \
  -d '{"name":"测试用户","age":25,"gender":"男"}' | jq '.' 2>/dev/null || curl -s -X POST "$BASE_URL" \
  -H "Content-Type: application/json" \
  -d '{"name":"测试用户","age":25,"gender":"男"}'
echo ""

# 测试修改用户
echo "5. 测试修改用户..."
curl -s -X PUT "$BASE_URL/1" \
  -H "Content-Type: application/json" \
  -d '{"name":"修改后的用户","age":26,"gender":"男"}' | jq '.' 2>/dev/null || curl -s -X PUT "$BASE_URL/1" \
  -H "Content-Type: application/json" \
  -d '{"name":"修改后的用户","age":26,"gender":"男"}'
echo ""

# 测试异常处理
echo "6. 测试异常处理（用户名为空）..."
curl -s -X POST "$BASE_URL" \
  -H "Content-Type: application/json" \
  -d '{"age":25,"gender":"男"}' | jq '.' 2>/dev/null || curl -s -X POST "$BASE_URL" \
  -H "Content-Type: application/json" \
  -d '{"age":25,"gender":"男"}'
echo ""

echo "=== 测试完成 ===" 